package binaryTree;

import java.util.*;

/**
 * @author kunkun
 * @className LeetCode_102
 * @Description ...
 * @date 2025/3/14 14:15
 */
public class LeetCode_102 {

    public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode() {}
       TreeNode(int val) { this.val = val; }
       TreeNode(int val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
       }
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        return solution_1(root);
    }

    private List<List<Integer>> solution_1(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();

        queue.add(root);
        queue.add(null);
        ArrayList<List<Integer>> lists = new ArrayList<List<Integer>>();
        List<Integer> list = new ArrayList<>();
        while (!queue.isEmpty()){
            TreeNode node = queue.remove();
            if (node==null){
                break;
            }
            list.add(node.val);

            if (node.left!=null){
                queue.add(node.left);
            }
            if (node.right!=null){
                queue.add(node.right);
            }
            if (queue.element()==null){
                lists.add(list);
                list = new ArrayList<>();
                queue.remove();
                queue.add(null);
            }
        }
        return lists;


    }


}
